
<template>
  <el-dialog v-model="ifShow" :title="modelTitle" width="1000" :before-close="handleCancel">
    <el-form ref="ruleForm" :model="formData" :rules="rules" label-width="auto" class="demo-ruleForm" status-icon>
      <el-form-item v-if="modelTitle == '开票'" label="订单号" prop="orderId">
        <el-input placeholder="请输入" v-model="formData.orderId" />
      </el-form-item>
      <el-form-item label="开票类型" prop="invoiceType">
        <el-select v-model="formData.invoiceType" placeholder="请选择" size="large">
          <el-option :value="1" label="企业"></el-option>
          <el-option :value="2" label="个人"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="抬头" prop="title">
        <el-input placeholder="请输入" v-model="formData.title" />
      </el-form-item>
      <el-form-item label="税号" prop="taxNumber">
        <el-input placeholder="请输入" v-model="formData.taxNumber" />
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input placeholder="请输入" v-model="formData.address" />
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input placeholder="请输入" v-model="formData.phone" />
      </el-form-item>
      <el-form-item label="开户银行" prop="bank">
        <el-input placeholder="请输入" v-model="formData.bank" />
      </el-form-item>
      <el-form-item label="银行账户" prop="bankAccount">
        <el-input placeholder="请输入" v-model="formData.bankAccount" />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input placeholder="请输入" v-model="formData.remark" />
      </el-form-item>
      <el-form-item label="邮箱" prop="email">
        <el-input placeholder="请输入" v-model="formData.email" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleCancel">取消</el-button>
        <el-button type="primary" @click="handleOk">确认</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import CommonApi from "@/api/common";
import OrderApi from "@/api/order";

const modelTitle = ref("开票");
const ruleForm = ref(null);
const orderId = ref();
const formData = reactive({
  invoiceId: "",
  orderId: "",
  invoiceType: "",
  title: "",
  taxNumber: "",
  address: "",
  phone: "",
  bank: "",
  bankAccount: "",
  remark: "",
  email: "",
});
const rules = reactive({
  invoiceType: [{ required: true, message: "请选择", trigger: "blur" }],
  title: [{ required: true, message: "请输入", trigger: "blur" }],
});

const ifShow = ref(false);
const emit = defineEmits(["success"]);
function edit(title: any, row: any) {
  nextTick(() => {
    modelTitle.value = title;
    if (row) {
      formData.invoiceId = row.id;
    }
    ifShow.value = true;
  });
}

/**
 * 确定按钮点击事件
 */
async function handleOk() {
  await ruleForm.value.validate((valid, fields) => {
    if (valid) {
      if (modelTitle.value == "开票") {
        OrderApi.invoiceCreate(formData).then((res) => {
          if (res.code == 0) {
            emit("success");
            handleCancel();
            ElMessage.success(res.message);
            ruleForm.value.resetFields();
          }
        });
      } else {
        OrderApi.invoiceRcreate(formData).then((res) => {
          if (res.code == 0) {
            emit("success");
            handleCancel();
            ElMessage.success(res.message);
            ruleForm.value.resetFields();
          }
        });
      }
    } else {
      console.log("error submit!", fields);
    }
  });
}

/**
 * 取消按钮回调事件
 */
function handleCancel() {
  ifShow.value = false;
}

defineExpose({
  edit,
});
onMounted(() => {});
</script>
<style lang="scss" scoped>
.btn_case {
  padding: 14px 0;
  .el-button {
    width: 100px;
    margin: 14px 14px 14px 0;
  }
}
</style>
